package com.weixing.mall.provider.controller.front;


import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.pay.channel.wx.WxPaySet;
import com.weixing.mall.pay.enums.PayChannelEnum;
import com.weixing.mall.provider.api.model.vo.PaySetVo;
import com.weixing.mall.provider.model.domain.PayChannel;
import com.weixing.mall.provider.service.IPayChannelService;
import com.weixing.mall.provider.service.IPaySetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 支付接口
 *
 * @author siashan
 * @author siashan
 * @since v1
 */
@RestController
@RequestMapping("front/payset")
public class FrontPaySetController extends BaseController {

    @Autowired
    private IPayChannelService payChannelService;
    @Autowired
    private IPaySetService paySetService;

    /**
     * 支付渠道列表
     *
     * @apiNote 根据isEnable字段判断前端是否展示该支付
     * @return 渠道列表
     */
    @GetMapping("channel")
    public Result<List<PayChannel>> channelList() {
        QueryWrapper<PayChannel> queryWrapper = new QueryWrapper<PayChannel>().orderByAsc("id");
        List<PayChannel> list = payChannelService.list(queryWrapper);
        return ResultUtil.success(list);
    }

    /**
     * 动态查询微信公众号appid
     * @return appid
     */
    @GetMapping("wxPaySetInfo")
    public Result<String> loadPaySet() {
        PayChannel payChannel = payChannelService.selectByCode(PayChannelEnum.WX_PAY.getValue());
        if (null == payChannel || IsEnableEnum.DISABLE.getValue().intValue() == payChannel.getIsEnable()) {
            return ResultUtil.error(payChannel.getChannelName() + "已禁用");
        }
        List<PaySetVo> paySetVos = paySetService.selectEnableListByChannelId(payChannel.getId());
        PaySetVo paySetVo = null;
        if (CollUtil.isNotEmpty(paySetVos)) {
            Date now = new Date();
            // 如果可用配置只有一个就使用该配置，如果可用配置为多个，则选用订单创建日期在配置生效时间段内的配置
            int size = paySetVos.size();
            if (size == 1) {
                paySetVo = paySetVos.get(0);
            } else {
                for (int i = 0; i < size; i++) {
                    PaySetVo vo = paySetVos.get(i);
                    Date effectStartTime = vo.getEffectStartTime();
                    Date effectEndTime = vo.getEffectEndTime();
                    if (now.compareTo(effectStartTime) >= 0 && now.compareTo(effectEndTime) <= 0) {
                        paySetVo = vo;
                        break;
                    }
                }
            }

        }
        if (null != paySetVo) {
            WxPaySet wxPaySet = JSON.parseObject(paySetVo.getParamSet(), WxPaySet.class);
            return ResultUtil.successData(wxPaySet.getAppid());
        }
        return ResultUtil.error("微信支付配置错误");
    }
}
